/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import util.JTableUtil;

/**
 *
 * @author Kuchcik
 */
public class Patient {
    User user;
    String id_medyka;
    String PESEL;
    String Adres;
    String Nr_Ubezpieczenia;
    static JFrame parent;
    
    public Patient(User user, String m, String Pesel, String Adres, String Nr_U,JFrame frame){
        this.user=user;
        this.id_medyka=m;
        this.PESEL=Pesel;
        this.Adres=Adres;
        this.Nr_Ubezpieczenia=Nr_U;
        Patient.parent=frame;
    }
    
    public boolean addPatient(Patient patient, String password) throws SQLException{
        
         Statement stmt = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        try{
            stmt.execute("insert into users values("+patient.user.getAuthorizationID()+",'"+patient.user.getLogin()+"','"+
            password+"','"+patient.user.getFirstName()+"','"+patient.user.getLastName()+"','"+patient.user.getEMail()+"',"+
            Integer.toString(1)+");");           
            
            stmt.execute("select id from users where login= '"+patient.user.getLogin()+"'");
            ResultSet rs = stmt.getResultSet();
            rs.next();
            int pat_id = rs.getInt(1);
            
            stmt.execute("insert into pacjenci values ("+pat_id+","+Integer.parseInt(patient.id_medyka)+",'"+patient.PESEL+"','"+patient.Adres+"','"+patient.Nr_Ubezpieczenia+"')");
            
            return true;
        }
        catch(SQLException ex)
        {
            ErrorLogger.getInstance().log("Bład połączenia z bazą danych", true, user.getJFrameParent());
        }
               
        
        return false;
    }
    
    public static ResultSet getPatient(int patientID) throws SQLException
    {
        Statement stmt = SqlConnection.getInstance().getSqlConnection().createStatement(
            ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
        try{
            stmt.execute(""
                    + "SELECT ID,U.imie AS IMIE , U.nazwisko AS NAZWISKO, "
                    + " P.pesel AS PESEL,P.adres AS ADRES,P.nr_ubezpieczenia AS 'NUMER UBEZPIECZENIA' , "
                    + " U.email AS 'E-MAIL' "
                    + " FROM pacjenci P JOIN users U"
                    + " ON P.id_user=U.id "
                    + " WHERE id_user = '"+patientID+"';");   
            ResultSet rs = stmt.getResultSet();
            return rs;
        }
        catch(SQLException ex)
        {
            ErrorLogger.getInstance().log("Bład połączenia z bazą danych", true, parent);
        }
        return null;
    }
    
    public static ResultSet getPatientHistory(int idPatient)
    {
        try{
            PreparedStatement stmt = SqlConnection.getInstance().getSqlConnection().prepareStatement(
                    "SELECT H.id, U.imie + ' ' +U.nazwisko AS Lekarz,H.data AS Data , H.rozpoznanie AS Rozpoznanie,H.zalecenia AS Zalecania "
                    + "FROM historia_wizyt H JOIN users U ON H.id_medyka = U.id "
                    + "WHERE id_pacjenta=?",
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmt.setInt(1, idPatient);
            return stmt.executeQuery();
        }
         catch (SQLException ex ) {
            ErrorLogger.getInstance().log(ex.getMessage(), true,parent);
        }       
        return null;
    }
    
    public static boolean deletePatient(String eMail){
      
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("select id from users where email='"+eMail+"'");
            ResultSet rs = stmn.getResultSet();
            rs.next();
            int id_user = rs.getInt(1);
            
            stmn.execute("delete from users where id="+id_user);
            
            return true;
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log(ex.getMessage(), true,parent);
        }           
        return false;
    }
}
